package com.easy.mq.config;

import com.easy.common.component.ExchangeType;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {

    @Bean
    public Queue queueA() {
        return new Queue("queueA");
    }
    @Bean
    public Queue queueB() {
        return new Queue("queueB");
    }
    @Bean
    public Queue queueC() {
        return new Queue("queueC");
    }
    @Bean
    public Queue queueD() {
        return new Queue("queueD");
    }
    @Bean
    public Queue queuesk() {
        return new Queue("SKQ");
    }

    @Bean
    public Binding binding_Topic_SKQ(@Qualifier("queuesk") Queue queueSKQ, TopicExchange topicExchange){
        return BindingBuilder.bind(queueSKQ).to(topicExchange).with("sk.#");
    }

    /**
     * 默认交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(ExchangeType.directExchange.toString());
    }
    /**
     * 扇出交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange (ExchangeType.fanoutExchange.toString());
    }
    /**
     * 头交换机
     * @return
     */
    @Bean
    public HeadersExchange headersExchange() {
        return new HeadersExchange (ExchangeType.headersExchange.toString());
    }

    /**
     * 主题交换器
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(ExchangeType.topicExchange.toString());
    }


    @Bean
    public Binding binding_Direct_QueueA(@Qualifier("queueA") Queue easyQueueA, DirectExchange directExchange) {
        //设定的式默认交换机，必须明确指定绑定的路由键
        //若交换机未绑定任何队列，或消息的 Routing Key 未匹配任何已绑定的队列，消息会被交换机直接丢弃
        return BindingBuilder.bind(easyQueueA).to(directExchange).with("easyMQA");
    }

    @Bean
    public Binding binding_Direct_QueueC(@Qualifier("queueC") Queue queueC, DirectExchange directExchange) {
        //设定的式默认交换机，必须明确指定绑定的路由键
        //若交换机未绑定任何队列，或消息的 Routing Key 未匹配任何已绑定的队列，消息会被交换机直接丢弃
        return BindingBuilder.bind(queueC).to(directExchange).with("easyMQC");
    }

    @Bean
    public Binding binding_Fanout_QueueA(@Qualifier("queueA") Queue easyQueueA, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(easyQueueA).to(fanoutExchange);
    }

    @Bean
    public Binding binding_Fanout_QueueB(@Qualifier("queueB") Queue easyQueueB, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(easyQueueB).to(fanoutExchange);
    }

    @Bean
    public Binding binding_Topic_QueueA(@Qualifier("queueC") Queue queueC, TopicExchange topicExchange){
        return BindingBuilder.bind(queueC).to(topicExchange).with("easy.#");
    }
    @Bean
    public Binding binding_Topic_QueueB(@Qualifier("queueD") Queue queueD, TopicExchange topicExchange){
        return BindingBuilder.bind(queueD).to(topicExchange).with("easy.*");
    }

    @Bean
    public Binding binding_Header_All_QueueA(HeadersExchange headersExchange, @Qualifier("queueA") Queue easyQueueA) {
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("type", "message");
        headerMap.put("status", "active");
        return BindingBuilder.bind(easyQueueA)
                .to(headersExchange)
                .whereAll(headerMap) // x-match=all
                .match();
    }

    @Bean
    public Binding binding_Header_Any_QueueB(HeadersExchange headersExchange, @Qualifier("queueB") Queue easyQueueB) {
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("type", "message");
        headerMap.put("status", "active");
        return BindingBuilder.bind(easyQueueB)
                .to(headersExchange)
                .whereAny(headerMap) // x-match=any
                .match();
    }

    //消费者方法
//    @RabbitListener(queues = "queueA")
//    public void listenterA(String message) {
//
//        System.out.println("easyQueueA-------------"+message);
//    }
//    @RabbitListener(queues = "queueB")
//    public void listenterB(String message) {
//
//        System.out.println("easyQueueB-------------"+message);
//    }
//    @RabbitListener(queues = "queueC")
//    public void listenterC(String message) {
//
//        System.out.println("easyQueueC-------------"+message);
//    }
//    @RabbitListener(queues = "queueD")
//    public void listenterD(String message) {
//
//        System.out.println("easyQueueD-------------"+message);
//    }

}
